<?php
/**
 * Created by b17
 * 2/15/12 8:56 PM
 */

namespace Managers;

class DeliveryMailer
{
    /**
     * @var Mailer
     */
    protected $mailer;

    public function __construct()
    {
        $this->mailer = new Mailer();
    }

    public function send()
    {
        $messagesManager = new DeliveryMessages();
        $messages = $messagesManager->getUndeliveredList();
        $emailsManager = new DeliveryEmail();
        $emails = $emailsManager->getList();
        $sentBuilder = new \Managers\Delivery\SentBuilder();
        foreach ($messages as $message) {
            foreach ($emails as $email) {
                $mail = new \Zend_Mail();
                $mail->setBodyHtml($message['content']);
                $mail->setSubject($message['title']);
                $mail->addTo($email['email']);

                $this->mailer->addMessage($mail);
                $sentBuilder->add($email['id'], $message['id']);
            }
            $this->mailer->send();
            $sentBuilder->write();
            $messagesManager->setDelivered($message['id']);
        }

    }
}
